package com.juli.v2xdemo.tcp.protocol;

import android.util.Log;

import com.cz.basetool.CodeTool;
import com.cz.basetool.ui_work.util.log.AndroidConsoleLogPrinter;
import com.juli.v2xdemo.entity.CarInfo;
import com.juli.v2xdemo.entity.MessageEvent;
import com.juli.v2xdemo.tcp.controller.TcpController;
import com.juli.v2xdemo.tcp.protocol.bean.CMD_55AA;
import com.juli.v2xdemo.tcp.protocol.bean.CarSetInfoBean;
import com.juli.v2xdemo.tcp.protocol.bean.CmdConstants;

import de.greenrobot.event.EventBus;


/**
 * Created by XinYi on 2019/5/21.
 * 55AA协议指令具体处理
 */
public class Cmd55AABusinesser {
    private static final String TAG = "Cmd55AABusinesser";
    private static final Cmd55AABusinesser ourInstance = new Cmd55AABusinesser();

    public static Cmd55AABusinesser getInstance() {
        return ourInstance;
    }

    private OnResultCallBack<Integer> onSetCarTypeResultCallBack;
    private OnResultCallBack<Integer> onSetCarStatusResultCallBack;
    private OnResultCallBack<CarSetInfoBean> onGetCarInfoResultCallBack;
    private OnResultCallBack<Integer> onStopUploadCarInfoResultCallBack;

    private Cmd55AABusinesser() {

    }

    public void listen() {
        ProtocolDispatcher.getInstance().setOnServerResponseResult(new ProtocolDispatcher.OnServerResponseResult() {
            @Override
            public void onResponse(boolean success, CMD_55AA cmd_55AA) {
                if (cmd_55AA==null) {
                    Log.e(TAG, "数据异常");
                    return;

                }
                final String message = cmd_55AA.getDATA();


                switch (cmd_55AA.getCMD()) {
                    case CmdConstants.CMD_TYPE_SET_CAR_TYPE_RESPONSE:
                        Log.e(TAG, "data:"+message);

                        CarInfo carData=new CarInfo();
                        carData.setVehichlInfo(message.substring(0,2));
                        carData.setVehiID(message.substring(2,18));
                        carData.setLatitude(message.substring(18,26));
                        carData.setLongitude(message.substring(26,34));
                        carData.setLat_Long_Dir(message.substring(34,36));
                        carData.setHeading(message.substring(36,40));
                        carData.setSpeed(message.substring(40,44));
                        carData.setAlertType(message.substring(44,48));
                        carData.setDescription(message.substring(48,60));
                        carData.setLane_num(message.substring(60,62));
                        carData.setVeh_Lane(message.substring(62,64));
                        MessageEvent messageEvent=new MessageEvent();
                        messageEvent.setType("data");
                        messageEvent.setCarInfo(carData);
                        EventBus.getDefault().post(messageEvent);
                        //打印数据 32
                        //拆解报文  data  55AA 0020 08 B0 00  00000000000000005775E51756033E450000000000000000000000 D4D7
//
                        break;

                    default:
                        Log.e(TAG, "unknown cmd = : " + cmd_55AA.getCMD());
                        break;
                }
            }
        });
    }


    /**
     * 设置车辆类型   0非紧急车辆  1紧急车辆
     */
    public void setCarType(int vehiType, OnResultCallBack<Integer> onSetCarTypeResultCallBack) {
        this.onSetCarTypeResultCallBack = onSetCarTypeResultCallBack;
        boolean result = TcpController.getInstance().sendMsg(CMD_55AA.create(
                CmdConstants.CMD_TYPE_SET_CAR_TYPE_REQUEST,
                CodeTool.num2HexStr(vehiType, 1)).getCMDSTR()
        );
        if(result == false){
            onSetCarTypeResultCallBack.onFailure();
        }
    }

    /**
     * 设置车辆状态  0正常 1故障
     */
    public void setCarStatus(int vehiStatus, OnResultCallBack<Integer> onSetCarStatusResultCallBack) {
        this.onSetCarStatusResultCallBack = onSetCarStatusResultCallBack;
        boolean result = TcpController.getInstance().sendMsg(CMD_55AA.create(
                CmdConstants.CMD_TYPE_SET_CAR_STATE_REQUEST,
                CodeTool.num2HexStr(vehiStatus, 1)).getCMDSTR()
        );
        if(result == false){
            onSetCarStatusResultCallBack.onFailure();
        }
    }

    /**
     * 获取车辆信息
     */
    public void getCarInfo(OnResultCallBack<CarSetInfoBean> onGetCarInfoResultCallBack) {
        AndroidConsoleLogPrinter.d("getCarInfo");
        this.onGetCarInfoResultCallBack = onGetCarInfoResultCallBack;
        TcpController.getInstance().sendMsg(CMD_55AA.create(
                CmdConstants.CMD_TYPE_GET_CAR_INFO_REQUEST,
                "").getCMDSTR()
        );
    }

    /**
     * 开始上传车辆信息
     */
    public void startUploadCarInfo() {
        AndroidConsoleLogPrinter.d("startUploadCarInfo");
        TcpController.getInstance().sendMsg(CMD_55AA.create(
                CmdConstants.CMD_TYPE_START_UPLOAD_CAR_TYPE_REQUEST,
                "").getCMDSTR()
        );
    }

    /**
     * 停止上传车辆信息
     */
    public void stopUploadCarInfo(OnResultCallBack<Integer> onStopUploadCarInfoResultCallBack) {
        AndroidConsoleLogPrinter.d(TAG,"stopUploadCarInfo");
        this.onStopUploadCarInfoResultCallBack = onStopUploadCarInfoResultCallBack;
        TcpController.getInstance().sendMsg(CMD_55AA.create(
                CmdConstants.CMD_TYPE_STOP_UPLOAD_CAR_TYPE_REQUEST,
                "").getCMDSTR()
        );
    }


    public interface OnResultCallBack<T> {
        void onSuccess(T t);

        void onFailure();

        void onException(Throwable e);
    }
}
